//Documentation complete
/**
 * @author Jason Funk
 * @version 2009FE19
 */

package problemDomain;

import problemDomain.ErrorBean;
import static problemDomain.ErrorBean.throwError;
import static problemDomain.ErrorBean.ErrorCode.*;
import static utilities.XMLTools.encodeEntities;

/**
 * Represents a food item type.
 */
public class FoodItemTypeBean implements Bean {
	
	/* END Constants */
	
	private static final long serialVersionUID = -2963855683435029061L;
	
	/* END Constants */
	
	
	/* Attributes */
	
	private int 	id;
	private String 	name;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>FoodItemTypeBean</code> with default values.
	 */
	public FoodItemTypeBean () {
		id = 0;
		name = "";
	}

	/**
	 * Constructs a <code>FoodItemTypeBean</code> with specified ID number and other default values.
	 * This is purposed for searching.
	 * @param id the ID number of the food item type.
	 */
	public FoodItemTypeBean (int id) {
		this();
		this.id = id;
	}
	
	/**
	 * Constructs a <code>FoodItemTypeBean</code> with a name value.
	 * This is purposed for searching.
	 * @param name the name of the food item type.
	 */
	public FoodItemTypeBean (String name) {
		this();
		this.name = name;
	}
	
	/**
	 * Constructs a <code>FoodItemTypeBean</code> with specified values.
	 * @param id the ID number of the food item type.
	 * @param name the name of the food item type.
	 */
	public FoodItemTypeBean (int id, String name) {
		this.id = id;
		this.name = name;
	}
	
	/* END Constructors */
	
	
	/* Accessors */
	
	/**
	 * Returns the ID number of the food item type.
	 * @return The ID number of the food item type
	 */
	public int getId () {
		return id;
	}

	/**
	 * Returns the name of the food item type.
	 * @return the name of the food item type.
	 */
	public String getName () {
		return name;
	}

	/* END Accessors */
	
	
	/* Mutators */
	
	/**
	 * Sets the ID number of the food item type.
	 * @param id The ID number of the food item type
	 */
	public void setId (int id) {
		this.id = id;
	}
	
	/**
	 * Sets the name of the food item type.
	 * @param name The name of the food item type
	 */
	public void setName (String name) {
		this.name = name;
	}
	
	/* END Mutators */
	
	
	/* (Bean) Methods */
	
	@Override
	public boolean isValid () throws ErrorBean {
		if (id < 0)
			throwError(
				ITEMTYPE_ID_INVALID,
				String.valueOf(id),
				this);
		
		if (name.length() > 25)
			throwError(
				ITEMTYPE_NAME_INVALID,
				name,
				this);
		
		return true;
	}
	
	@Override
	public String toXML () {
		StringBuilder sb = new StringBuilder();
		sb.append("<fooditemtype>");
		sb.append("<id>" + id + "</id>");
		sb.append("<name>" + name + "</name>");
		sb.append("</fooditemtype>");
		return encodeEntities(sb.toString());
	}
	
	/* END (Bean) Methods */
	
	
	/* (Object) Methods */
	
	@Override
	public String toString () {
		StringBuilder sb = new StringBuilder();
		sb.append("ID: " + id + '\n');
		sb.append("Food Item Type Name: " + name + "\n\n");
		return sb.toString();
	}
	
	/* END (Object) Methods */
	
}
